package view;

import control.AdminKontrol;
import control.LoginKontrol;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.Divisi;
import model.Pegawai;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Asus
 */
public class HomePageDesktop extends javax.swing.JFrame {

    /**
     * Creates new form HomePageDesktop
     */
    public HomePageDesktop() {
        initComponents();
        tampilComboDivisi();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textUsername = new javax.swing.JTextField();
        labelHomePage = new javax.swing.JLabel();
        labelUsername = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        labelDivisi = new javax.swing.JLabel();
        comboDivisi = new javax.swing.JComboBox();
        tombolMasukHomePage = new javax.swing.JButton();
        tombolKeluarHomePage = new javax.swing.JButton();
        textPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelHomePage.setText("HOME PAGE");

        labelUsername.setText("Username ");

        labelPassword.setText("Password  ");

        labelDivisi.setText("Divisi        ");

        comboDivisi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tombolMasukHomePage.setText("Masuk");
        tombolMasukHomePage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tombolMasukHomePageActionPerformed(evt);
            }
        });

        tombolKeluarHomePage.setText("Keluar");
        tombolKeluarHomePage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tombolKeluarHomePageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(223, 223, 223)
                .addComponent(labelHomePage)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(labelDivisi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                            .addComponent(labelUsername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(132, 132, 132))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(125, 125, 125)
                        .addComponent(tombolKeluarHomePage)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(tombolMasukHomePage)
                        .addGap(49, 49, 49))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(177, 177, 177)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textUsername)
                            .addComponent(comboDivisi, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(textPassword))))
                .addGap(83, 83, 83))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelHomePage)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelUsername))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPassword)
                    .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDivisi)
                    .addComponent(comboDivisi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tombolMasukHomePage)
                    .addComponent(tombolKeluarHomePage))
                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tombolMasukHomePageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolMasukHomePageActionPerformed
        // TODO add your handling code here:
        Pegawai peg = new Pegawai();
        Divisi div = new Divisi();
        peg.setUsername(textUsername.getText());
        peg.setPassword(textPassword.getText());
        String id = comboDivisi.getItemAt(comboDivisi.getSelectedIndex()).toString();
        div.setIdDivisi(id.substring(0, 3));
        peg.setDivisi(div);

        try {
            LoginKontrol.getKoneksiKategori().login(peg);
        } catch (SQLException ex) {
            Logger.getLogger(frameAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (peg.getUsername().matches("danik") && peg.getPassword().matches("danik")
                && peg.getDivisi().getIdDivisi().matches("001")) {
            JOptionPane.showMessageDialog(null, "Selamat Datang \nAnda berhasil Login!");
            this.dispose();
            frameAdmin frame = new frameAdmin();
            frame.setVisible(true);
        } else if (peg.getUsername().matches("renia") && peg.getPassword().matches("renia") && peg.getDivisi().getIdDivisi().matches("004")) {
            JOptionPane.showMessageDialog(null, "Selamat Datang \nAnda berhasil Login!");
            this.dispose();
            frameDaftarTagihan frame = new frameDaftarTagihan();
            frame.setVisible(true);
        } else if (peg.getUsername().matches("orin") && peg.getPassword().matches("orin") && peg.getDivisi().getIdDivisi().matches("003")) {
            JOptionPane.showMessageDialog(null, "Selamat Datang \nAnda berhasil Login!");
            this.dispose();
            frameDaftarPesanan frame = new frameDaftarPesanan();
            frame.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "Login Gagal");
        }
        textUsername.setText("");
        textPassword.setText("");
    }//GEN-LAST:event_tombolMasukHomePageActionPerformed

    private void tombolKeluarHomePageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolKeluarHomePageActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_tombolKeluarHomePageActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HomePageDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HomePageDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HomePageDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HomePageDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HomePageDesktop().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboDivisi;
    private javax.swing.JLabel labelDivisi;
    private javax.swing.JLabel labelHomePage;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JLabel labelUsername;
    private javax.swing.JPasswordField textPassword;
    private javax.swing.JTextField textUsername;
    private javax.swing.JButton tombolKeluarHomePage;
    private javax.swing.JButton tombolMasukHomePage;
    // End of variables declaration//GEN-END:variables

    private void tampilComboDivisi() {
        try {
            comboDivisi.removeAllItems();
            List<Divisi> daftardivisis = AdminKontrol.getKoneksiKategori().tampilDivisiPegawai();
            for (int i = 0; i < daftardivisis.size(); i++) {
                Divisi divisi = daftardivisis.get(i);
                comboDivisi.addItem(divisi.getIdDivisi() + " " + divisi.getNamaDivisi());
            }
        } catch (SQLException ex) {
            Logger.getLogger(frameAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
